<?php

$pagename = $headTitle = fl('faq');
$catId = isset($_GET['cat']) ? intval($_GET['cat']) : 0;
$questionId = isset($_GET['id']) ? intval($_GET['id']) : 0;
$isQuestion = $isCat = $isCatList = false;

// display question
if ($questionId > 0) {

    if ('en' === Lang::getCode()) {
        $fields = 'c_response2 AS c_response, c_request2 AS c_request';
    } else {
        $fields = 'c_response, c_request';
    }

    $row = Db::getOneRow($fields, 'contact', "contactid = $questionId");

    $content = $row['c_response'];
    $headTitle = $row['c_request'] . ' | ' . $pagename;

    $isQuestion = true;
}

// display category list
else
{
    $perPage = 10;
    $pageNum = isset($_GET['p']) ? max($_GET['p'], 1) : 1;
    $orderby='c_date_posted DESC';
    $conditions="c_type=2 AND c_status=1";

    if ($catId > 0) {
        $conditions .= " AND contact_category_id = $catId";

        if ('en' === Lang::getCode()) {
            $catFields = 'name2 AS name';
        } else {
            $catFields = 'name';
        }

        $cat = Db::getOneRow($catFields, 'contact_category', "id = $catId");

        $headTitle = $cat['name'] . ' | ' . $pagename;
    }

    if (isset($_GET['s']) && !empty($_GET['s'])) {
        if ('en' === Lang::getCode()) {
            $conditions .= " AND MATCH (c_request2, c_response2) AGAINST ('" . mysql_escape_string($_GET['s']) . "') ";
        } else {
            $conditions .= " AND MATCH (c_request, c_response) AGAINST ('" . mysql_escape_string($_GET['s']) . "') ";
        }
        $keywords = htmlspecialchars($_GET['s']);
    }

    if ('en' === Lang::getCode()) {
        $fields = 'c_request2 AS c_request, c_response2 AS c_response';
        $catFields = 'name2 AS name';
    } else {
        $fields = 'c_request, c_response';
        $catFields = 'name';
    }

    if ($pageNum < 1) {
        $pageNum = 1;
    }

    $totalRows = Db::getOneField('COUNT(*)', 'contact', $conditions);

    $totalPages = ceil($totalRows/$perPage);

    $rows = db()->getAll("SELECT *,$fields FROM contact WHERE $conditions ORDER BY $orderby LIMIT " . ($pageNum-1)*$perPage . ", $perPage", MYSQL_ASSOC);

    $paging = Tpl::page($totalRows, $perPage, "support?cat=$catId" . (isset($_GET['s']) ? '&' . urlencode($_GET['s']) : '') . "&p=");

    if ('en' === Lang::getCode()) {
        $fields = 'name2 AS name';
        $sorts = 'name2';
    } else {
        $fields = 'name';
        $sorts = 'name';
    }

    $catList = db()->getAll("SELECT *, $fields FROM contact_category ORDER BY `order`, $sorts ASC", MYSQL_ASSOC);
}

$supportList = array(
        1 => array(),
        2 => array(),
        4 => array(),
        5 => array()
);

if ('en' === Lang::getCode()) {
    $supportFields = 's_name2 AS s_name';
    $supportSort = 's_name2';
} else {
    $supportFields = 's_name';
    $supportSort = 's_name';
}

$supportRows = Db::getRows('*, ' . $supportFields, 'support', 's_group IN (1,2,4,5)', $supportSort . ' ASC');

foreach ($supportRows as $row)
{
    if (!empty($row['s_skype'])) {
        //$row['skype_status'] = file_get_contents('http://mystatus.skype.com/' . $row['s_skype'] . '.txt');
        $row['skype_status'] = 'Offline';
    }
    $supportList[$row['s_group']][] = $row;
}

?>
